// 连续数组
class Solution {
    public:
        int findMaxLength(vector<int>& nums) {
            int n = nums.size();
            for(int i = 0; i < n; i++)
            {
                if(nums[i] == 0)
                {
                    nums[i] = -1;
                }
            }
            unordered_map<int,int>hash;
            int sum = 0;
            int len = 0;
    
            for(int i = 0; i < n; i++)
            {
                sum += nums[i];
                if(hash.count(sum) > 0)
                {
                    len =  max(len,i - hash[sum]);
                }
                else
                {
                    hash[sum] = i;
                    cout << sum << " " <<hash[sum] << endl;
                }
                if(sum == 0) len = max(i + 1, len);
            }
            return len;
        }
    };

    // 矩阵区域和
    class Solution {
        public:
            vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {
                int rows = mat.size();
                int cols = mat[0].size();
                vector<vector<int>> num(rows + 1,vector<int>(cols + 1));
        
                for(int row = 1; row <= rows; row++)
                {
                    for(int col = 1; col <= cols; col++)
                    {
                        num[row][col] = num[row-1][col] + num[row][col-1] - num[row-1][col-1] + mat[row-1][col-1];
                    }
                }
                vector<vector<int>> ret(rows,vector<int>(cols));
                for(int row = 0; row < rows; row++)
                {
                    for(int col = 0; col < cols; col++)
                    {
                        int x1 = max(0, row - k) + 1;
                        int y1 = max(0, col - k) + 1;
                        int x2 = min(rows - 1, row + k) + 1;
                        int y2 = min(cols - 1, col + k) + 1;
                        ret[row][col] = num[x2][y2] - num[x1 - 1][y2] - num[x2][y1 - 1] + num[x1-1][y1-1];
                    }
                }
        
                return ret;
            }
        };